<!-- user表单弹窗 -->
<form id="userForm" lay-filter="userForm" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input name="userCode" placeholder="请输入账号" type="text" class="layui-input" maxlength="50"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input name="userName" placeholder="请输入用户名" type="text" class="layui-input" maxlength="50"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input name="mobile" placeholder="请输入手机号" type="text" class="layui-input" lay-verify="myPhone"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="0" title="男" checked/>
            <input type="radio" name="sex" value="1" title="女"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <input name="photoUrl" placeholder="请输入头像" type="text" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">默认主页</label>
        <div class="layui-input-block">
            <input name="defaultPage" placeholder="请输入默认主页" type="text" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="userForm-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['form','admin','http','tools'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var admin = layui.admin;
        var tools = layui.tools;
        var http = layui.http;
        var param = tools.param;

        var viewId = '#userForm';
        var view = $(viewId);

        form.render('radio','userForm');

        form.verify({
            myPhone: function (value,item) {
                if(value){
                    if(!layui.form.config.verify.phone[0].test(value)){
                        return layui.form.config.verify.phone[1];
                    }
                }
            }
        });

        // 回显user数据
        if(param.rowObj && param.rowObj.data){
            form.val('userForm',param.rowObj.data);
        }

        // 表单提交事件
        form.on('submit(userForm-submit)', function (data) {
            http.post('user/save', data.field, function (res) {
                if(res.ok){
                    if(param.action===2){
                        // param.rowObj.update(res.data);
                        // var index = param.rowObj.tr.attr("data-index");
                        // param.tableIns.resetDelta(index);
                    }else if(param.action===1){
                        param.tableIns.addRow(res.data,null,true);
                    }
                    http.okMsg(res.msg);
                    admin.closeDialog('#userForm');
                }
            });
            return false;
        });

    });
</script>